// pages/coach_mana/main.js

var con = require('../../utils/request.js')
var ui_tool = require('../../utils/uitool.js')

Page({
    data: {
        car_type_list: ["不指定", "科二练习车", "科三练习车"],
        car_type_index: 0,
        page_index: 0,
        banner_files: "/img/cusbanner.png",
        default_banner_file: "/img/cusbanner.png",
        form_new_car: {
            car_no: "",
            student_count: 1,
            practice_type: 2,
            car_coach_name: "",
            car_coach_tel: "",
        },
        list_car: [],
        elements: getApp().menus
    },
    previewImage: function (e) {
        wx.previewImage({
            current: e.currentTarget.id, // 当前显示图片的http链接
            urls: this.data.files // 需要预览的图片http链接列表
        })
    },
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                that.setData({
                    banner_files: that.data.banner_files = (res.tempFilePaths[0])
                });
            }
        })
    },
    commit_form: function () {
        // 正式功能
        if (this.data.form_new_car.car_no == "") {
            ui_tool.show_error("请输入有效的车牌号");
            return;
        }

        if (this.data.form_new_car.car_coach_name == "" || this.data.form_new_car.car_coach_tel == "") {
            ui_tool.show_error("请输入有效的随车教练信息")
            return;
        }


        if (this.data.banner_files != this.data.default_banner_file) {
            wx.showLoading({
                title: "图片上传中..."
            })
            con.upload(this.data.banner_files, {}, this);
        }
        else {
            // 用户没有设置图片，提交
            this.create_car(undefined);
        }
    },
    btn_new_car: function () {
        this.data.form_new_car = {
            car_no: "",
            student_count: 1,
            practice_type: 2,
            car_coach_name: "",
            car_coach_tel: "",
        };
        this.data.banner_files = this.data.default_banner_file;
        this.setData({
            page_index: 1
        });
    },
    back_list_page: function () {
        this.setData({
            page_index: 0
        });
    },
    create_car: function (image) {
        wx.showLoading({
            title: "创建中..."
        });
        var cp_form = this.data.form_new_car;

        cp_form.images = []
        if (image != undefined)
            cp_form.images.push(iamge)
        con.remote_call("api/create_car", cp_form, this);
    },
    on_request_complate: function (cmd, data, status, msg) {
        wx.hideToast();
        if (status == 200) {
            if (cmd == "api/list_car") {
                console.info(data);
                this.setData({
                    list_car: data
                })
            }
            else if (cmd == "api/create_car") {
                wx.showToast({
                    title: '创建成功',
                    icon: 'success',
                    duration: 2000
                });
                this.back_list_page();
                con.remote_call("api/list_car", {}, this);
            }
        }
        else {
            ui_tool.show_error(msg);
        }
    },
    on_upload_complate: function (file, data, status, msg) {
        wx.hideToast();
        if (status == 200) {
            this.create_car(data.guid)
        }
        else {
            ui_tool.show_error(msg);
        }
    },
    on_cartype_change: function (e) {
        this.setData({
            car_type_index: e.detail.value
        })
    },
    inputChange: function (e) {
        var ids = e.target.id;
        var content = {};
        content[ids] = e.detail.value;
        this.setData(content)
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        con.remote_call("api/list_car", {}, this);
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示

    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    }
})